#!/usr/bin/awk -f
function get_host_addr()
{
    cmd = "ifconfig|awk '/inet/&&!/inet6/&&!/127.0.0.1/{print $2}'|awk 'BEGIN {FS = \":\"} {print $2}'";
    ip_addr = (system(cmd)|getline line);
    print "LLLL"line;
}
function dumpsummary(stat, currtime, time_interval)
{
    if (stat["total"] != 0)
    {
        user_succ = stat["success"]
        sys_succ = stat["total"] - stat["servfail"] 
        failnum = stat["total"] - stat["success"] - stat["nxdomain"]
        query_ratio = stat["total"]/((time_interval/ 100)*60);
        sys_succ_ratio = sys_succ*100/stat["total"];
        user_succ_ratio = user_succ*100/stat["total"];
        delay_avg = stat["delay"]/stat["total"];
        printf("====================SUMMARY====================\n");
        printf("starttime:\t%s\n", (currtime));
        printf("endtime:\t%s\n", currtime + time_interval);
        printf("dnsserverip:\t%s\n", ipaddr);
        printf("user_succ:\t%d\n", user_succ);
        printf("sys_succ:\t%d\n", sys_succ);
        printf("failnum:\t%d\n", failnum);
        printf("recursion num:\t%d\n", stat["recursive"]);
        printf("no-recursion:\t%d\n", stat["total"] - stat["recursive"]);
        printf("total query:\t%d\n", stat["total"]);
        printf("frequency:\t%f\n", query_ratio);
        printf("sys_succ%:\t%f\n", sys_succ_ratio);
        printf("user_succ%:\t%f\n", user_succ_ratio);
        printf("delay-avg:\t%f\n", delay_avg);
        printf("==================================================\n");
    }
}
function cleandomain(dn)
{
    if (substr(dn, length(dn) ) == ".")
        dn = substr(dn, 1, length(dn)-1);

    split(dn, temparr, ".");    
    lens = length(temparr);
    i = lens - 1;
    tempdn = sprintf("%s.%s",temparr[i], temparr[i+1]);
    bexist = 0;
    for (e in top_words)
    {
        if (top_words[e] == tempdn)
        {
            bexist = 1;
            break;
        }
    }
    if (bexist == 0)
        return tempdn;
    else
        return sprintf("%s.%s", temparr[i-1], tempdn);
}
function sort(top, new_top, seq_domain)
{
    count = 0;
    for (dn in top)
    {
        split(dn, t, SUBSEP);
        if (t[2] == "count")
            count++;
    }
    for (i=1;i <= count;i++)
    {
        max = 0;
        for (dn in top)
        {
            split(dn, tarr, SUBSEP);
            if (tarr[2] == "count")
            {
                if (max < top[tarr[1],"count"])
                { 
                    max = top[tarr[1],"count"];
                    domain = tarr[1];
                }
            }
        }
        seq_domain[i] = domain 
        new_top[domain,"count"] = top[domain, "count"] 
        new_top[domain,"user_fail"] = top[domain, "user_fail"] 
        new_top[domain,"servfail"] = top[domain, "servfail"] 
        new_top[domain,"success"] = top[domain, "success"] 
        new_top[domain,"nxdomain"] = top[domain, "nxdomain"] 
        new_top[domain,"refuse"] = top[domain, "refuse"] 
        new_top[domain,"no-imp"] = top[domain, "no-imp"] 
        delete top[domain, "count"] 
        delete top[domain, "user_fail"] 
        delete top[domain, "servfail"] 
        delete top[domain, "success"] 
        delete top[domain, "nxdomain"] 
        delete top[domain, "refuse"] 
        delete top[domain, "no-imp"] 
    }
    delete top
}
function dumptop(top, currtime, time_interval)
{
    if (length(top) != 0)
    {
        printf("====================TOP====================\n");
        printf("starttime|endtime|serverip|domain-name|request|total|ratio|success%\n");
        delete new_top;
        delete seq_domain; 
        sort(top, new_top, seq_domain); 
        total = 0;
        for (e in seq_domain)
        {
            total += new_top[seq_domain[e],"count"]
        }

        #for (e in seq_domain)
        for (e = 1; e <= length(seq_domain); e++)
        {
            printf("%s|", (currtime));
            printf("%s|", currtime + time_interval);
            printf("%s|", ipaddr);
            printf("%s|", seq_domain[e]);
            printf("%s|", new_top[seq_domain[e],"count"]);
            printf("%s|", total);
            printf("%f|", new_top[seq_domain[e],"count"]*100.0/total);
            printf("%f\n", (new_top[seq_domain[e],"success"] + new_top[seq_domain[e],"nxdomain"])*100.0/new_top[seq_domain[e],"count"]);
        }
    }
}
function initsummary(stat)
{
    stat["success"] = 0;
    stat["user_fail"] = 0;
    stat["servfail"] = 0;
    stat["nxdomain"] = 0;
    stat["no-imp"] = 0;
    stat["refuse"] = 0;
    stat["recursive"] = 0;
    stat["total"] = 0;
    stat["delay"] = 0.0;
}
BEGIN {
    FS = "|";
    ipaddr = ip; 
    lasttime = "20111010101010";
    time_interval = 500;
    top_words_str = "com.cn,gov.cn,edu.cn,org.cn,net.cn,edu.tw,com.br,com.ua,net.br,com.tw,com.hk,co.uk,co.jp";
    split(top_words_str, top_words, ","); 
}
{
    starttime = $3;
    interval = (starttime - lasttime)
    if (interval > time_interval) 
    {
        dumpsummary(stat, lasttime, time_interval);
        dumptop(top, lasttime, time_interval);
        initsummary(stat);

        lasttime = starttime;
    }

    {
        stat["total"]++;
        stat["delay"] += $6;
        domain = cleandomain($2);
        top[domain,"count"] += 1;
        if ($6 != 0)
        {
            stat["recursive"]++;
        }
        if ($5 == 0)
        {
            stat["success"]++;
            top[domain,"success"] +=1;
        }
        else if ($5 == 1)
        {
            stat["user_fail"]++;
            top[domain,"user_fail"] +=1;
        }
        else if ($5 == 2)
        {
            stat["servfail"]++;
            top[domain,"servfail"] +=1;
        }
        else if ($5 == 3)
        {
            stat["nxdomain"]++;
            top[domain,"nxdomain"] +=1;
        }
        else if ($5 == 4)
        {
            stat["no-imp"]++;
            top[domain,"no-imp"] +=1;
        }
        else if ($5 == 5)
        {
            stat["refuse"]++;
            top[domain,"refuse"] +=1;
        }
    }
}
END {
}

